﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using HYCodeBase.Algorithm;

namespace HYRevitCode.CurveUtility.IntersectionUtility
{
	public static class FastCurveIntersectionUtility
	{
		public static List<CurveIntersectionResult> FastCurveIntersection(List<Curve> input)
		{
			List<CurveIntersectionResult> list = new List<CurveIntersectionResult>();
			HashSet<CurveIntersectionResult> hashSet = new HashSet<CurveIntersectionResult>();
			List<CurveData> list2 = new List<CurveData>();
			foreach (Curve inputCurve in input)
			{
				list2.Add(new CurveData(inputCurve));
			}
			foreach (KeyValuePair<CurveData, List<CurveData>> keyValuePair in TreeSearchUtility.NearSearch<CurveData>(list2))
			{
				foreach (CurveData curveData in keyValuePair.Value)
				{
					CurveIntersectionResult curveIntersectionResult = new CurveIntersectionResult(keyValuePair.Key.ThisCurve, curveData.ThisCurve);
					if (!hashSet.Contains(curveIntersectionResult))
					{
						hashSet.Add(curveIntersectionResult);
						IntersectionResultArray intersectionResultArray;
						if ((int)keyValuePair.Key.ThisCurve.Intersect(curveData.ThisCurve, out intersectionResultArray) == 8)
						{
							curveIntersectionResult.IntersectionResultArray = intersectionResultArray;
							list.Add(curveIntersectionResult);
						}
					}
				}
			}
			return list;
		}
	}
}
